package com.tsd.jxc.stock.service.impl;

import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.thread.ThreadPoolTaskUtil;
import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.stock.dao.JxcStockOutbillItemMapper;
import com.tsd.jxc.stock.dao.JxcStockOutbillMapper;
import com.tsd.jxc.stock.entity.JxcStockOutbillExt;
import com.tsd.jxc.stock.entity.JxcStockOutbillItem;
import com.tsd.jxc.stock.entity.JxcStockOutbillItemExt;
import com.tsd.jxc.stock.service.JxcStockOutbillItemService;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcStockOutbillItem 服务实现层
 *
 * @author Hillpool
 * @date 2022/07/29 15:06:33
 */
@Service
public class JxcStockOutbillItemServiceImpl extends BaseServiceImpl implements JxcStockOutbillItemService {

    @Resource
    private JxcStockOutbillItemMapper jxcStockOutbillItemMapper;
    @Resource
    private JxcStockOutbillMapper jxcStockOutbillMapper;

    @Override
    public JxcStockOutbillItem getJxcStockOutbillItemById(Long id) throws Exception {
        return jxcStockOutbillItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public JxcStockOutbillItemExt queryBySid(String sid) throws Exception {
        return jxcStockOutbillItemMapper.selectBySid(sid);
    }

    @Override
    public List<JxcStockOutbillItemExt> queryValidByBillList(List<String> bill_list) throws Exception {
        List<JxcStockOutbillItemExt> itemExtList = ThreadPoolTaskUtil.queryInList4Split(bill_list, list -> {
            JxcStockOutbillItemExt params = new JxcStockOutbillItemExt();
            params.setDr(0);
            params.setBill_list(list);
            return jxcStockOutbillItemMapper.selectByEntity(params);
        });
        return itemExtList;
    }

    @Override
    public void deleteJxcStockOutbillItems(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            JxcStockOutbillItem old = jxcStockOutbillItemMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            jxcStockOutbillItemMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveJxcStockOutbillItem(JxcStockOutbillItemExt record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            jxcStockOutbillItemMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            jxcStockOutbillItemMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void saveJxcStockOutbillItem(JxcStockOutbillExt record, SysUser opUser) throws Exception {
        List<JxcStockOutbillItemExt> itemList = record.getItem_list();
        super.checkEmptyList(itemList, "单据明细不能为空");

        List<Long> existIdList = ListUtil.filterMap(itemList, t -> !HlpUtils.isEmpty(t.getId()), JxcStockOutbillItemExt::getId);

        JxcStockOutbillItemExt params = new JxcStockOutbillItemExt();
        params.setDr(0);
        params.setOutbill_sid(record.getSid());
        List<JxcStockOutbillItemExt> oldList = jxcStockOutbillItemMapper.selectByEntity(params);

        //删除不存在订单明细
        List<JxcStockOutbillItemExt> delList = ListUtil.filter(oldList, t -> !existIdList.contains(t.getId()));
        if (!HlpUtils.isEmptyList(delList)) {
            List<Long> delIds = ListUtil.map(delList, JxcStockOutbillItemExt::getId);
            BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
            logicVo.setIdList(delIds);
            jxcStockOutbillItemMapper.deleteLogicByIdList(logicVo);

            List<String> delSids = ListUtil.map(delList, JxcStockOutbillItemExt::getSid);
            logicVo = new BatchDeleteLogicVo(opUser);
            logicVo.setSidList(delSids);
        }

        double totalQty = 0D;
        for (JxcStockOutbillItemExt itemExt : itemList) {
            super.checkEmpty(itemExt.getOut_qty(), "第" + itemExt.getSn() + "项明细数量不能为空");
            super.checkEmpty(itemExt.getPrice(), "第" + itemExt.getSn() + "项明细价格不能为空");
            itemExt.setOutbill_sid(record.getSid());
            if (HlpUtils.isEmpty(itemExt.getAmount())) {
                double amount = DoubleUtil.mul(itemExt.getOut_qty(), itemExt.getPrice());
                itemExt.setAmount(BigDecimal.valueOf(amount));
            }
            totalQty = DoubleUtil.add(totalQty, itemExt.getOut_qty());
            this.saveJxcStockOutbillItem(itemExt, opUser);
        }
        record.setOut_qty(BigDecimal.valueOf(totalQty));
        jxcStockOutbillMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<JxcStockOutbillItemExt> findJxcStockOutbillItems(Map<String, Object> params, PageBean page) throws Exception {
        return jxcStockOutbillItemMapper.selectByPage(params, page);
    }

    @Override
    public List<JxcStockOutbillItemExt> findJxcStockOutbillItems(Map<String, Object> params) throws Exception {
        return jxcStockOutbillItemMapper.selectByParams(params);
    }
}
